package com.hsy.directseeding.my;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.hsy.directseeding.R;
import com.hsy.directseeding.account.LoginActivity;
import com.hsy.directseeding.common.CommonBaseActivity;
import com.hsy.directseeding.common.Consts;
import com.hsy.directseeding.utils.CommonUtils;
import com.hsy.directseeding.utils.HttpUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/9.
 */
public class ChangePasswordActivity extends CommonBaseActivity {
    protected EditText passEdit;
    protected EditText newPassEdit;
    protected EditText passAgainEdit;
    protected TextView changePassword;
    private DPHttpUtil.HttpCallback callBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            showToast(response);
            CommonUtils.hideProgressDialog();
            CommonUtils.clearToken(ChangePasswordActivity.this);
            startActivityWithTaskCleared(LoginActivity.class);
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("修改密码");
        super.setContentView(R.layout.activity_change_password);
        initView();
        final List<EditText> editText = new ArrayList<>();
        final List<String> msg = new ArrayList<>();
        editText.add(passEdit);
        editText.add(newPassEdit);
        editText.add(passAgainEdit);
        msg.add("请输入原密码");
        msg.add("请输入新密码");
        msg.add("请确认密码");
        changePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtils.checkLog(ChangePasswordActivity.this, editText, msg)) {
                    if (newPassEdit.getText().toString().equals(passAgainEdit.getText().toString())) {
                        CommonUtils.showProgressDialog(ChangePasswordActivity.this);
                        Map<String, String> map = new HashMap<String, String>();
                        map.put("password_old", passEdit.getText().toString());
                        map.put("password_new", newPassEdit.getText().toString());
                        map.put("token", CommonUtils.getToken(ChangePasswordActivity.this));
                        HttpUtil.httpPostWithParams(ChangePasswordActivity.this, 0, Consts.Apis.POST_CHANGE_PASSWORD, map, callBack);
                    } else {
                        showToast("两次密码不一致");
                    }
                }
            }
        });
    }

    private void initView() {
        passEdit = (EditText) findViewById(R.id.pass_edit);
        newPassEdit = (EditText) findViewById(R.id.new_pass_edit);
        passAgainEdit = (EditText) findViewById(R.id.pass_again_edit);
        changePassword = (TextView) findViewById(R.id.change_password);
    }
}
